from pymongo import MongoClient


class mongoDb:
    def __init__(self, doc):
        try:
            self.conn = MongoClient('192.168.13.1', 27017)
            self.db = self.conn["tycGysDb"]
            self.doc = self.db[doc]
        except:
            print('conn tycGysDb is err')

    def insert_one(self, item):
        try:
            self.doc.insert_one(item)
            print('insert %s is ok!!!' % item)
            return True
        except Exception:
            print(Exception)
            return False

    def insert(self, itemList):
        try:
            self.doc.insert(itemList)
            print('insert %s is ok!!!' % itemList)
            return True
        except Exception:
            print(Exception)
            return False

    def find_one(self, arg):
        print('this find_one is %s documents find by %s' % (self.doc, arg))
        try:
            item = self.doc.find_one(arg)
            return item
        except Exception:
            print('arg %s is not found!!!' % arg)
            return {}

    def find(self, arg={}):
        try:
            print('this is debug info:-------:this find() is %s documents find by %s' % (self.doc, arg))
            items = self.doc.find(arg)
            print('this is debug info:-------:the count is %d' % self.doc.find().count())
            return items
        except Exception:
            print('this is debug info:-------:arg %s is not found!!!' % arg)
            return []

    # 按条件查查并指定显示哪个字段的集合 返回list
    def findOutList(self, key, arg={}):
        revList = []
        try:
            print('this is debug info:-------:this findOutList() is %s documents find by %s' % (self.doc, arg))
            items = self.doc.find(arg, {key: 1})
            for item in items:
                if isinstance(item[key], list):
                    for sub in item[key]:
                        revList.append(sub)
                else:
                    revList.append(item)
        except Exception:
            print('this is debug info:-------:arg %s is not found!!!' % arg)
        return revList

    def findTop(self, top, key, isAsc, arg={}):
        print(top, key, isAsc)
        try:
            print('this is debug info:-------:this findTop() is find doc %s  and arg is :%s' % (self.doc, arg))
            print('find the top is :%d  and order by key is %s isAsc :%s' % (top, key, isAsc))
            print('the sql is self.doc.find(%s).limit(%d).sort({%s: %d})' % (arg, top, key, isAsc))
            items = self.doc.find(arg).limit(top).sort(key, isAsc)
            # print('this is debug info:-------:the count is %d' % self.doc.find(arg).limit(top).sort(key, isAsc).count()
            return items
        except Exception:
            print('this is debug info:-------:arg %s is not found!!!' % arg)
            print(Exception)
            return []

    def update_one(self, filer, item):
        try:
            self.doc.update_one(filer, {"$set": item})
            print('{0}.update_one {1} is ok!!!'.format(self.doc))
            return True
        except:
            return False

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.conn.close()
        # if __name__ == '__main__':
        #     with mongoDb('industry') as industry:
        #         print(industry.findOutList("secList",{}))
